package com.dingreading.cloud.soms.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.dto.CorpTeacherDto;
import com.dingreading.cloud.common.dto.LoginDataDto;
import com.dingreading.cloud.common.dto.StaffRoleDto;
import com.dingreading.cloud.common.util.AccountUtil;
import com.dingreading.cloud.common.util.CookiesUtil;
import com.dingreading.cloud.common.util.LoginAuthUtil;
import com.dingreading.cloud.common.util.RedisParam;
import com.dingreading.cloud.soms.entity.Staff;
import com.dingreading.cloud.soms.entity.StoreMenu;
import com.dingreading.cloud.soms.entity.StoreRole;
import com.dingreading.cloud.soms.service.StaffService;
import com.dingreading.cloud.soms.service.StoreMenuService;
import com.dingreading.cloud.soms.service.StoreRoleService;
import com.dingreading.cloud.soms.service.StoresService;
import com.dingreading.cloud.soms.util.LoginInfoUtil;
import com.dingreading.cloud.soms.util.TreeJsonUtil;
import com.dingreading.cloud.soms.util.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/front/staff")
@Api(tags = "门店-员工管理")
public class FrontStaffController {

    @Resource
    protected StoresService storesService;
    @Resource
    protected StaffService staffService;
    @Resource
    protected StoreMenuService menuService;
    @Resource
    protected StoreRoleService rolesService;
    @Resource
    private RedisService redisService;


    @ApiOperation(value = "根据门店uid和员工uid获取信息")
    @PostMapping(value = "/getStaff", produces = "application/json; charset=UTF-8")
    public R<Object> getStaff(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName
    ) {
        Staff staff = staffService.getByUid(staffUid);

        if (staff != null) {
            return R.ok(staff);
        } else {
            return R.fail("获取员工失败");
        }
    }

    @ApiOperation(value = "获取门店的员工")
    @PostMapping(value = "/getStoreStaff", produces = "application/json; charset=UTF-8")
    public R<Object> getStoreStaff(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled,
            @ApiParam(value = "姓名/手机号") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "nickName不能为空 1.不能为空") @RequestParam(value = "nameBlank", required = false) Integer nameBlank
    ) {
        List<JSONObject> staffs = staffService.listByStoreStaff(storeUid, enabled, keyword, null);
        return R.ok(staffs);
    }

    @ApiOperation(value = "获取门店的员工(不包含市场人员)")
    @PostMapping(value = "/getStoreStaffNotMarketer", produces = "application/json; charset=UTF-8")
    public R<Object> getStoreStaffNotMarketer(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled,
            @ApiParam(value = "姓名/手机号") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "nickName不能为空 1.不能为空") @RequestParam(value = "nameBlank", required = false) Integer nameBlank
    ) {
        List<JSONObject> staffs = staffService.listByStoreStaff(storeUid, enabled, keyword, 0);
        return R.ok(staffs);
    }

    @ApiOperation(value = "获取公司的市场员工")
    @PostMapping(value = "/getCorpMarketer", produces = "application/json; charset=UTF-8")
    public R<Object> getCorpMarketer(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled,
            @ApiParam(value = "姓名/手机号") @RequestParam(value = "keyword", required = false) String keyword
    ) {
        List<Staff> staffs = staffService.listByCorpMarketerV2(staffUid, enabled, keyword);
        return R.ok(staffs);
    }

    @ApiOperation(value = "获取公司下的主讲导读")
    @PostMapping(value = "/getCorpTeachers", produces = "application/json; charset=UTF-8")
    public R<Object> getCorpTeachers(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "姓名/手机号") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        if (StringUtils.isNotBlank(keyword)) {
            if (keyword.length() < 2)
                return R.fail("查询关键字至少填写2位");
        }

        String redisKey = RedisParam.CORP_TEACHER + storeUid;
        String json = (String) redisService.get(redisKey);

        List<CorpTeacherDto> list = new ArrayList<>();
        if (StringUtils.isNotBlank(json)) {
            list = JSONArray.parseArray(json, CorpTeacherDto.class);
        } else {
            list = staffService.getCorpTeachers(storeUid, keyword, null, 1);

            for (CorpTeacherDto object : list) {
                String fullName = object.getFullName();
                String nickName = object.getNickName();
                object.setNickName(StringUtils.isNotBlank(nickName) ? nickName : fullName);
            }

            // 缓存2小时
            json = JSONArray.toJSONString(list);
            redisService.set(redisKey, json, 600);
        }

        return R.ok(list);
    }

    @ApiOperation(value = "获取登录员工的菜单集合")
    @PostMapping(value = "/getMenus", produces = "application/json; charset=UTF-8")
    public R<Object> getMenus(
            HttpServletRequest request,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName
    ) {
        R<Object> r = LoginInfoUtil.getLoginDto(request);
        if (R.isError(r)) return r;
        LoginDataDto loginDto = (LoginDataDto) r.getData();

        // 0.普通员工 1.店长 2.市场
        StaffRoleDto roleDto = AccountUtil.getStaffRole(loginDto.getStoreRoles());
        if (null == roleDto)
            return R.fail("未发现员工的角色权限");

        // 根据角色标识获取菜单
        StoreRole storeRole = rolesService.getByIdentify(roleDto.getIdentify());
        if (null == storeRole)
            return R.fail("角色标识获取权限失败");

        String cooperationSql = AccountUtil.getCooperationTypeSql2(loginDto.getCurrentCooperationTypes());
        String operationTypeSql = AccountUtil.getOperationTypeSql2(loginDto.getCurrentOperationType());
        List<StoreMenu> menuList = menuService.listByRoleIdAndOperationType(storeRole.getId(), cooperationSql, operationTypeSql);
        JSONArray menuTree = new JSONArray();
        if (CollectionUtils.isNotEmpty(menuList))
            menuTree = TreeJsonUtil.createStoreMenuTreeJson(menuList);

        return R.ok(menuTree);
    }

    @ApiOperation(value = "清空门店员工的登录信息")
    @PostMapping(value = "/clearStaffInfo", produces = "application/json; charset=UTF-8")
    public R<Object> clearStaffInfo(
            HttpServletRequest request
    ) {
        Cookie cookie = CookiesUtil.getCookieByName(request, LoginAuthUtil.DDPB_TOKEN);
        if (cookie != null && StringUtils.isNotBlank(cookie.getValue())) {
            String cookieValue = cookie.getValue();
            redisService.setNullLike(RedisParam.STAFF_LOGIN + cookieValue);
        }

        return R.ok();
    }

}